//
//  FKBaseNavVC.m
//  GC_OC
//
//  Created by Macbook Pro on 2023/6/19.
//

#import "FKBaseNavVC.h"

@interface FKBaseNavVC ()<UINavigationBarDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) NSMutableDictionary *pushingDict;


@end

@implementation FKBaseNavVC

- (NSMutableDictionary *)pushingDict
{
    if (!_pushingDict) {
        _pushingDict = [NSMutableDictionary dictionary];
    }
    return _pushingDict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 解决右滑手势失效的问题
    __weak typeof(self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakSelf;
        self.delegate = (id)weakSelf;
    }
}

/**
 重写这个方法的目的：能够拦截所有push进来的控制器
 
 @param viewController 即将push进来的控制器
 @param animated 是否需要动画
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSString *key = NSStringFromClass(viewController.class);
    if ([self.pushingDict[key] boolValue]) return;
    self.pushingDict[key] = @(true);
    
    if (self.viewControllers.count > 0) { // 这时push进来的控制器，不是第一个子控制器（不是根控制器）
        // 自动显示和隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 发消息给tabbarController，让其隐藏视频播放器
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hideSmallPlayer" object:nil userInfo:nil];
    }
    
    [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSString *key = NSStringFromClass(viewController.class);
    self.pushingDict[key] = nil;
    
    if (self.childViewControllers.count == 1) {
        // 发消息给tabbarController，让其显示视频播放器
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showSmallPlayer" object:nil userInfo:nil];
    }
}


@end
